package com.chookss.view

import android.content.Context
import android.graphics.Color
import android.graphics.Point
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatDialog
import com.chookss.R

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2021/1/20 14:32
 * Note:
 */
open abstract class BottomDialog(context: Context,titleData:String):AppCompatDialog(context, R.style.DialogFullWidthDimEnabled) {
    lateinit var mRootView:View
    lateinit var tvTitleBottom:TextView
     var titleDatas:String=titleData
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mRootView = View.inflate(context, R.layout.dialog_black_bottom, null)
        mRootView.findViewById<ViewGroup>(R.id.llContent).addView(createContentView())
        mRootView.findViewById<ImageView>(R.id.ivClose).setOnClickListener { dismiss() }
        tvTitleBottom= mRootView.findViewById<TextView>(R.id.tvTitleBottom)
        mRootView.findViewById<TextView>(R.id.tvTitleBottom)?.text = titleDatas;
        setContentView(mRootView)
        val lp = window?.attributes
        val wm = window
        lp?.width = ViewGroup.LayoutParams.MATCH_PARENT
        lp?.height = ViewGroup.LayoutParams.WRAP_CONTENT
        lp?.dimAmount = 0f
        wm?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        wm?.setGravity(Gravity.BOTTOM)
        wm?.setWindowAnimations(R.style.DialogFullWidthDimNotEnabled)
        window?.attributes = lp
    }

    abstract fun createContentView():View

    fun useWhiteTheme(): BottomDialog {
        mRootView.setBackgroundResource(R.drawable.bottom_dialog_white_bg)
        mRootView.findViewById<TextView>(R.id.tvTitle).setTextColor(Color.BLACK)
        mRootView.findViewById<ImageView>(R.id.ivClose).setImageResource(R.drawable.ic_close_black_18dp)
        return this
    }
    fun useBlackTheme(): BottomDialog {
        mRootView.setBackgroundResource(R.drawable.bottom_dialog_black_bg)
        mRootView.findViewById<TextView>(R.id.tvTitle).setTextColor(Color.WHITE)
        mRootView.findViewById<ImageView>(R.id.ivClose).setImageResource(R.drawable.ic_close_white_18dp)
        return this
    }
    fun setTitle(text: String?): BottomDialog {
        mRootView.findViewById<TextView>(R.id.tvTitleBottom)?.text = text;
        return this
    }
//    fun setTitle(text: String?) {
//        tvTitleBottom?.text = text;
//        mRootView.findViewById<TextView>(R.id.tvTitleBottom).setTextColor(Color.WHITE)
//        mRootView.findViewById<TextView>(R.id.tvTitleBottom)?.text = text;
//    }

    fun screenSizeInPixel(context: Context): Point {
        val dm = context.resources.displayMetrics
        val widthPixels = dm.widthPixels
        val heightPixels = dm.heightPixels
        return Point(Math.min(widthPixels, heightPixels), Math.max(widthPixels, heightPixels))
    }
}